package com.zuoban.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.converter.ListOutputConverter;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;

@CrossOrigin
@RestController
public class ChatClientDemoController {


    private final ChatClient chatClient;

    public ChatClientDemoController(ChatClient chatClient) {
        this.chatClient = chatClient;
    }

    @GetMapping("chat")
    public Flux<String> chat(@RequestParam(defaultValue = "hello",required = false,name = "message") String message) {
        return chatClient.prompt().user(message).stream().content();
    }


    @GetMapping("chatResponse")
    public Flux<ChatResponse> chatResponse(@RequestParam(defaultValue = "hello",required = false,name = "message") String message) {
        return chatClient.prompt().user(message).stream().chatResponse();
    }

    @GetMapping("chatEntity")
    public List<String> chatEntity(@RequestParam(defaultValue = "hello",required = false,name = "message") String message) {
        return chatClient.prompt().user(message).call().entity(new ListOutputConverter());
    }
}
